package com.thread.exe;

import java.util.HashMap;
import java.util.Map;

public class Memoizer1<A, V> implements Computable<A, V> {
	private final Map<A, V> cache = new HashMap<A, V>();
	private final Computable<A, V> c;
	
	public Memoizer1(Computable<A, V> c) {
		super();
		this.c = c;
	}

	@Override
	public synchronized V compute(A arg) throws InterruptedException {
		V v = cache.get(arg);
		if(v == null) {
			V compute = c.compute(arg);
			cache.put(arg, compute);
			v = compute;
		}
		return v;
	}
}
